package com.jzo2o.market.config;

import com.jzo2o.redis.properties.RedisSyncProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 线程池配置
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-04-09 14:53
 */
@Configuration
public class ThreadPoolConfiguration {

	@Bean("syncDataToCouponThreadPool")
	public ThreadPoolExecutor synchronizeThreadPool(RedisSyncProperties redisSyncProperties) {

		// 线程池配置
		int corePoolSize = 1;
		int maximumPoolSize = redisSyncProperties.getQueueNum();
		long keepAliveTime = 60;
		TimeUnit unit = TimeUnit.SECONDS;
		BlockingQueue<Runnable> workQueue = new SynchronousQueue<>();
		ThreadFactory threadFactory = Executors.defaultThreadFactory();
		RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();

		// 创建线程池
		return new ThreadPoolExecutor(
				corePoolSize,
				maximumPoolSize,
				keepAliveTime,
				unit,
				workQueue,
				threadFactory,
				handler
		);

	}

}
